package com.project.XXXX.api;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.EmployeePtMangaeController;
import com.aldjob.controllerImpl.PtController;
import com.aldjob.controllerImpl.UserController;
import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.IPtjobMutual;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.tbl_dictMapper;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_ptjobMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.inter.tbl_workplaceMapper;
import com.project.XXXX.inter.tbl_worktimeMapper;
import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_employerinfo;
import com.project.XXXX.model.tbl_jobenroll;
import com.project.XXXX.model.tbl_ptjob;
import com.project.XXXX.model.tbl_ptjobWithBLOBs;
import com.project.XXXX.model.tbl_sysuser;
import com.project.XXXX.model.tbl_workplace;
import com.project.XXXX.model.tbl_worktime;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.JobEnrollString;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.tools.ProjectTools;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_jobenrollMapper;

@Controller
public class EmployeeManage implements IPtjobMutual {
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_worktimeMapper tbl_worktimeMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	@Resource
	private tbl_divisionMapper tbl_divisionMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	@Resource
	private tbl_ptjobMapper tbl_ptjobMapper;
	@Resource
	private tbl_dictMapper tbl_dictMapper;

	EmployeePtMangaeController uc;
	boolean first;

	void prepare(HttpServletResponse response) {
		if (first) {
			uc = new EmployeePtMangaeController(tbl_jobenrollMapper, tbl_ptjobMapper, tbl_dictMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	HttpSession getAppSession(String vaidatecodeid) {
		SessionContext sessionContext = SessionContext.getInstance();
		return sessionContext.getSession(vaidatecodeid);
	}

	public EmployeeManage() {
		first = true;
	}

	@RequestMapping(value = "/api/employeeManage/Request")
	void commitJobRequest(@RequestParam("id") int ptjobid, @RequestParam("access_token") String sessonid,HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess=getAppSession(sessonid);
		int status = uc.Request(ptjobid, sess);
		switch(status){
			case 200:
				map.put("status", "success");
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "please log in");
				break;
			case 409:
				map.put("status", "failure");
				map.put("error_description", "request send already");
				break;
			case 422:
				map.put("status", "failure");
				map.put("error_description", "please complete your information");
				break;
		}
		response.setStatus(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/employeeManage/getbyStatus")
	public void getDiffReqJob(@RequestParam("page") int page, @RequestParam("status") String status,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess=getAppSession(sessonid);
		List<JobEnrollString> st = uc.getbyStatus(page, status, sess);
		String json ;
		if (st == null){
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
			json = new Gson().toJson(map);
		}
		else{
			json = new Gson().toJson(st);
		}		
		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/api/employeeManage/getCountbyStatus")
	public void getDiffReqJobCount(@RequestParam("status") String status,@RequestParam("access_token") String sessonid, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess=getAppSession(sessonid);
		int st = uc.getCountbyStatus(status, sess);
		if (st == -1){
			map.put("status", "failure");
			map.put("error_description", "please log in");
			response.setStatus(403);
		}
		else{
			map.put("count", st+"");
		}
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/employeeMange/cancelRequest")
	public void changeJobStatus1(@RequestParam("reqid") int reqid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess=getAppSession(sessonid);
		int status = uc.cancelRequest(reqid, sess);
		switch(status){
			case 200:
				map.put("status", "success");
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "please log in");
				break;
			case 409:
				map.put("status", "failure");
				map.put("error_description", "not your request");
				break;
		}
		response.setStatus(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}
